<?php
class Admin_UserController
    extends Admin_Abstract
{
    public function viewAction()
    {
        $this->_permision('user');
        $this->loadLayout();
        $this->renderLayout();
    }

    public function indexAction()
    {
        $this->_permision('user');
        $this->loadLayout();
        $this->renderLayout();
    }

    public function saveAction()
    {
        $this->_permision('user');
        $data = $this->getRequest()->getParam('user',array());
        $continue = $this->getRequest()->getParam('continue',false);
        $session = new Model_Admin_Session();
        try {
            $userModel = new Model_Admin_User_Admin();

            $errors = array();

            $id = null;
            if(!empty($data['id'])) {
                $id = $data['id'];
                $userModel->load($data['id']);
                if(isset($data['set_password'])) {
                    if($userModel->getPassword()==$data['current_password']) {
                        if ($data['new_password']!=$data['new_confirmation']) {
                            $errors[] = 'New password and Confirmation not valid';
                        } else {
                            $data['password'] = $data['new_password'];
                        }
                    } else {
                        $errors[] = 'Cannot set new password, not valid current password';
                    }
                }
            }

            unset($data['id']);

            foreach ($data as $key=>$value) {
                $userModel->setData($key,$value);
            }

            if(!count($errors)) {
                $userModel->save();
                $session->addSuccess('User successfully saved');
            } else {
                foreach ($errors as $_error) {
                    $session->addError($_error);
                }
            }
        } catch (Zend_Exception $e) {
            $session->addError('Error saving user');
            $session->addError($e->getMessage());
        }

        if($continue) {
            if($id) {
                $this->_redirect('admin/user/view/item/'.$id);
            } else {
                $this->_redirect('admin/user/view/');
            }
        } else {
            $this->_redirect('admin/user/index/');
        }
    }

    public function deleteAction()
    {
        $this->_permision('user');
        $item = $this->getRequest()->getParam('item',null);
        $session = new Model_Admin_Session();

        if($item) {
            try {
                $model = new Model_Admin_User_Admin();
                $model->load($item);
                $model->delete();
                $session->addSuccess('User deleted');
            } catch (Exception $e) {
                $session->addError($e->getMessage());
                $session->addError('Cannot delete user');
            }
        } else {
            $session->addError('Cannot delete user');
        }

        $this->_redirect('admin/user/index/');
    }
}